package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：w
 * @date ：Created in 2019/7/30 0030
 * @description ：权限dao
 * @version: 1.0
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 分页获取权限信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPermissionByCondition")
    public PageResult findPermissionByCondition(@RequestBody  QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try{
            pageResult = permissionService.findPermissionByCondition(queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return pageResult;
        }catch(Exception e){
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @RequestMapping("/addOneRecord4Permission")
    public Result addOneRecord4Permission(@RequestBody Permission permission) {
        try {
            permissionService.addOneRecord4Permission(permission);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 更新权限
     * @param permission
     * @return
     */
    @RequestMapping("/updateOneRecord4Permission")
    public Result updateOneRecord4Permission(@RequestBody Permission permission) {
        try {
            permissionService.updateOneRecord4Permission(permission);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 基于ID,获取权限信息
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id) {
        try {
            Permission permission = permissionService.findPermissionById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, permission);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }

    }

    /**
     * 基于ID,删除权限
     * @param id
     * @return
     */
    @RequestMapping("/delOneRecord4Permission")
    public Result delOneRecord4Permission(Integer id) {
        try {
            permissionService.delOneRecord4Permission(id);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
